﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmCapNhatNhomVatTu
    Private _NhomVatTuEdit As VT_DM_NhomVatTu
    Public Property NhomVatTuEdit() As VT_DM_NhomVatTu
        Get
            Return _NhomVatTuEdit
        End Get
        Set(ByVal value As VT_DM_NhomVatTu)
            _NhomVatTuEdit = value
        End Set
    End Property

    Private _IDNhomVatTu As String
    Public Property IDNhomVatTu() As String
        Get
            Return _IDNhomVatTu
        End Get
        Set(ByVal value As String)
            _IDNhomVatTu = value
        End Set
    End Property

    Private _Action As String
    Public Property Action() As String
        Get
            Return _Action
        End Get
        Set(ByVal value As String)
            _Action = value
        End Set
    End Property

    Private Sub FrmCapNhatNhomVatTu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        'Ghi và đóng
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pSAVECLOSE, btnGhiVaDong) Then
            btnGhiVaDong_ItemClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub FrmCapNhatNhomVatTu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lbPhimTat.Text = GetLabelPhimTatCoBanCapNhatDanhMuc()
        btnGhiVaDong.Caption = BtnSaveAndCloseCaption
        btnThoat.Caption = BtnExitCaption
        Select Case Action
            Case vEditAction, vCopyAction
                If NhomVatTuEdit IsNot Nothing Then
                    txtMaNhomVatTu.Text = NhomVatTuEdit.MaNhom
                    txtTenNhomVatTu.Text = NhomVatTuEdit.TenNhom
                    txtGhiChu.Text = NhomVatTuEdit.GhiChu
                End If
        End Select
    End Sub
    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub btnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaDong.ItemClick
        Dim strErr = CheckSave()
        If Not String.IsNullOrEmpty(strErr) Then
            MsgInfo(strErr)
            Return
        End If
        If SaveData() Then
            Me.Close()
        End If
    End Sub
    Private Function SaveData() As Boolean
        Try
            Select Case Action
                Case vAddAction, vCopyAction
                    Dim objAdd As New VT_DM_NhomVatTu
                    objAdd.ID = CreateID()
                    objAdd.MaNhom = txtMaNhomVatTu.Text.Trim
                    objAdd.TenNhom = txtTenNhomVatTu.Text.Trim
                    objAdd.GhiChu = txtGhiChu.Text.Trim
                    objAdd.EditedOn = dbTV.GetDateTimeServer
                     
                    objAdd.InUsed = True
                    dbTV.VT_DM_NhomVatTus.InsertOnSubmit(objAdd)
                    iSubmitChange()
                Case vEditAction
                    Dim objEdit = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed AndAlso ds.ID = NhomVatTuEdit.ID Select ds).FirstOrDefault
                    objEdit.MaNhom = txtMaNhomVatTu.Text.Trim
                    objEdit.TenNhom = txtTenNhomVatTu.Text.Trim
                    objEdit.GhiChu = txtGhiChu.Text.Trim
                   
                    objEdit.EditedOn = dbTV.GetDateTimeServer
                    iSubmitChange()
            End Select
        Catch ex As Exception
            MsgErr(ex.Message)
            Return False
        End Try
        Return True
    End Function
    Private Function CheckSave() As String
        If String.IsNullOrEmpty(txtMaNhomVatTu.Text.Trim) Then
            txtMaNhomVatTu.Focus()
            Return "Bạn chưa nhập mã nhóm vật tư"
        End If
        If String.IsNullOrEmpty(txtTenNhomVatTu.Text.Trim) Then
            txtTenNhomVatTu.Focus()
            Return "Bạn chưa nhập tên nhóm vật tư"
        End If
        Select Case Action
            Case vAddAction, vCopyAction
                Dim obj = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed AndAlso ds.MaNhom = txtMaNhomVatTu.Text.Trim Select ds).FirstOrDefault
                If obj IsNot Nothing Then
                    Return "Trùng mã nhóm vật tư"
                End If
            Case vEditAction
                If NhomVatTuEdit.MaNhom <> txtMaNhomVatTu.Text.Trim Then
                    Dim obj = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed AndAlso ds.MaNhom = txtMaNhomVatTu.Text.Trim Select ds).FirstOrDefault
                    If obj IsNot Nothing Then
                        Return "Trùng mã nhóm vật tư"
                    End If
                End If
        End Select
        Return ""
    End Function
End Class